home *** CD-ROM | disk | FTP | other *** search
/ Skunkware 5 / Skunkware 5.iso / src / Tools / lynx-2.4 / WWW / Library / Implementation / HTStyle.c < prev    next >
Encoding:
C/C++ Source or Header  |  1995-06-28  |  8.8 KB  |  366 lines

  1. /*    Style Implementation for Hypertext            HTStyle.c
  2. **    ==================================
  3. **
  4. **    Styles allow the translation between a logical property
  5. **    of a piece of text and its physical representation.
  6. **
  7. **    A StyleSheet is a collection of styles, defining the
  8. **    translation necessary to
  9. **    represent a document. It is a linked list of styles.
  10. */
  11. #include "HTUtils.h"
  12. #include "HTStyle.h"
  13.  
  14. #include "LYLeaks.h"
  15.  
  16. /*    Create a new style
  17. */
  18. PUBLIC HTStyle* HTStyleNew NOARGS
  19. {
  20.     HTStyle * self = (HTStyle *)malloc(sizeof(*self));
  21.     memset((void *)self, 0, sizeof(*self));
  22.     self->font = (HTFont) 0;
  23.     self->color = 0;
  24.     return self;
  25. }
  26.  
  27. /*    Create a new style with a name
  28. */
  29. PUBLIC HTStyle* HTStyleNewNamed ARGS1 (CONST char *,name)
  30. {
  31.     HTStyle * self = HTStyleNew();
  32.     StrAllocCopy(self->name, name);
  33.     return self;
  34. }
  35.  
  36.  
  37. /*    Free a style
  38. */
  39. PUBLIC HTStyle * HTStyleFree ARGS1 (HTStyle *,self)
  40. {
  41.     if (self->name) free(self->name);
  42.     if (self->SGMLTag) free(self->SGMLTag);
  43.     free(self);
  44.     return 0;
  45. }
  46.  
  47.  
  48. #ifdef SUPPRESS  /* Only on the NeXT */
  49. /*    Read a style from a stream    (without its name)
  50. **    --------------------------
  51. **
  52. **    Reads a style with paragraph information from a stream.
  53. **    The style name is not read or written by these routines.
  54. */
  55. #define NONE_STRING "(None)"
  56. #define HTStream NXStream
  57.  
  58. HTStyle * HTStyleRead (HTStyle * style, HTStream * stream)
  59. {
  60.     char myTag[STYLE_NAME_LENGTH];
  61.     char fontName[STYLE_NAME_LENGTH];
  62.     NXTextStyle *p;
  63.     int    tab;
  64.     int gotpara;        /* flag: have we got a paragraph definition? */
  65.     
  66.     NXScanf(stream, "%s%s%f%d",
  67.     myTag,
  68.     fontName,
  69.     &style->fontSize,
  70.     &gotpara);
  71.     if (gotpara) {
  72.     if (!style->paragraph) {
  73.         style->paragraph = malloc(sizeof(*(style->paragraph)));
  74.         style->paragraph->tabs = 0;
  75.     }
  76.     p = style->paragraph;
  77.     NXScanf(stream, "%f%f%f%f%hd%f%f%hd",
  78.         &p->indent1st,
  79.         &p->indent2nd,
  80.         &p->lineHt,
  81.         &p->descentLine,
  82.         &p->alignment,
  83.         &style->spaceBefore,
  84.         &style->spaceAfter,
  85.         &p->numTabs);
  86.     if (p->tabs) free(p->tabs);
  87.     p->tabs = malloc(p->numTabs * sizeof(p->tabs[0]));
  88.     for (tab=0; tab < p->numTabs; tab++) {
  89.         NXScanf(stream, "%hd%f",
  90.             &p->tabs[tab].kind,
  91.             &p->tabs[tab].x);
  92.     }
  93.     } else { /* No paragraph */
  94.         if (style->paragraph) {
  95.             free(style->paragraph);
  96.             style->paragraph = 0;
  97.     }
  98.     } /* if no paragraph */
  99.     StrAllocCopy(style->SGMLTag, myTag);
  100.     if (strcmp(fontName, NONE_STRING)==0)
  101.         style->font = 0;
  102.     else
  103.         style->font = [Font newFont:fontName size:style->fontSize];
  104.     return 0;
  105.  
  106. }
  107.  
  108.  
  109. /*    Write a style to a stream in a compatible way
  110. */
  111. HTStyle * HTStyleWrite (HTStyle * style, NXStream * stream)
  112. {
  113.     int tab;
  114.     NXTextStyle *p = style->paragraph;
  115.     NXPrintf(stream, "%s %s %f %d\n",
  116.     style->SGMLTag,
  117.     style->font ? [style->font name] : NONE_STRING,
  118.     style->fontSize,
  119.     p!=0);
  120.  
  121.     if (p) {
  122.     NXPrintf(stream, "\t%f %f %f %f %d %f %f\t%d\n",
  123.         p->indent1st,
  124.         p->indent2nd,
  125.         p->lineHt,
  126.         p->descentLine,
  127.         p->alignment,
  128.         style->spaceBefore,
  129.         style->spaceAfter,
  130.         p->numTabs);
  131.         
  132.     for (tab=0; tab < p->numTabs; tab++)
  133.         NXPrintf(stream, "\t%d %f\n",
  134.             p->tabs[tab].kind,
  135.             p->tabs[tab].x);
  136.     }
  137.     return style;
  138. }
  139.  
  140.  
  141. /*    Write a style to stdout for diagnostics
  142. */
  143. HTStyle * HTStyleDump (HTStyle * style)
  144. {
  145.     int tab;
  146.     NXTextStyle *p = style->paragraph;
  147.     printf("Style %d `%s' SGML:%s. Font %s %.1f point.\n",
  148.         style,
  149.     style->name,
  150.     style->SGMLTag,
  151.     [style->font name],
  152.     style->fontSize);
  153.     if (p) {
  154.         printf(
  155.         "\tIndents: first=%.0f others=%.0f, Height=%.1f Desc=%.1f\n"
  156.     "\tAlign=%d, %d tabs. (%.0f before, %.0f after)\n",
  157.         p->indent1st,
  158.         p->indent2nd,
  159.         p->lineHt,
  160.         p->descentLine,
  161.         p->alignment,
  162.         p->numTabs,
  163.         style->spaceBefore,
  164.         style->spaceAfter);
  165.         
  166.     for (tab=0; tab < p->numTabs; tab++) {
  167.         printf("\t\tTab kind=%d at %.0f\n",
  168.             p->tabs[tab].kind,
  169.             p->tabs[tab].x);
  170.         }
  171.     printf("\n");
  172.     } /* if paragraph */
  173.     return style;
  174. }
  175. #endif
  176.  
  177.  
  178. /*            StyleSheet Functions
  179. **            ====================
  180. */
  181.  
  182. /*    Searching for styles:
  183. */
  184. HTStyle * HTStyleNamed ARGS2 (HTStyleSheet *,self, CONST char *,name)
  185. {
  186.     HTStyle * scan;
  187.     for (scan=self->styles; scan; scan=scan->next)
  188.         if (0==strcmp(scan->name, name)) return scan;
  189.     if (TRACE) fprintf(stderr, "StyleSheet: No style named `%s'\n", name);
  190.     return 0;
  191. }
  192.  
  193. #ifdef NEXT_SUPRESS        /* Not in general common code */
  194.  
  195. HTStyle * HTStyleMatching (HTStyleSheet * self, HTStyle *style)
  196. {
  197.     HTStyle * scan;
  198.     for (scan=self->styles; scan; scan=scan->next)
  199.         if (scan->paragraph == para) return scan;
  200.     return 0;
  201. }
  202.  
  203. /*    Find the style which best fits a given run
  204. **    ------------------------------------------
  205. **
  206. **    This heuristic is used for guessing the style for a run of
  207. **    text which has been pasted in. In order, we try:
  208. **
  209. **    A style whose paragraph structure is actually used by the run.
  210. **    A style matching in font
  211. **    A style matching in paragraph style exactly
  212. **    A style matching in paragraph to a degree
  213. */
  214.  
  215. HTStyle * HTStyleForRun (HTStyleSheet *self, NXRun *run)
  216. {
  217.     HTStyle * scan;
  218.     HTStyle * best = 0;
  219.     int    bestMatch = 0;
  220.     NXTextStyle * rp = run->paraStyle;
  221.     for (scan=self->styles; scan; scan=scan->next)
  222.         if (scan->paragraph == run->paraStyle) return scan;    /* Exact */
  223.  
  224.     for (scan=self->styles; scan; scan=scan->next){
  225.         NXTextStyle * sp = scan->paragraph;
  226.         if (sp) {
  227.         int match = 0;
  228.         if (sp->indent1st ==    rp->indent1st)    match = match+1;
  229.         if (sp->indent2nd ==    rp->indent2nd)    match = match+2;
  230.         if (sp->lineHt ==        rp->lineHt)    match = match+1;
  231.         if (sp->numTabs ==        rp->numTabs)    match = match+1;
  232.         if (sp->alignment ==    rp->alignment)    match = match+3;
  233.         if (scan->font ==        run->font)    match = match+10;
  234.         if (match>bestMatch) {
  235.             best=scan;
  236.             bestMatch=match;
  237.         }
  238.     }
  239.     }
  240.     if (TRACE) fprintf(stderr, "HTStyleForRun: Best match for style is %d out of 18\n",
  241.                  bestMatch);
  242.     return best;
  243. }
  244. #endif
  245.  
  246.  
  247. /*    Add a style to a sheet
  248. **    ----------------------
  249. */
  250. HTStyleSheet * HTStyleSheetAddStyle ARGS2
  251.   (HTStyleSheet *,self, HTStyle *,style)
  252. {
  253.     style->next = 0;        /* The style will go on the end */
  254.     if (!self->styles) {
  255.         self->styles = style;
  256.     } else {
  257.         HTStyle * scan;
  258.         for(scan=self->styles; scan->next; scan=scan->next); /* Find end */
  259.     scan->next=style;
  260.     }
  261.     return self;
  262. }
  263.  
  264.  
  265. /*    Remove the given object from a style sheet if it exists
  266. */
  267. HTStyleSheet * HTStyleSheetRemoveStyle ARGS2
  268.   (HTStyleSheet *,self, HTStyle *,style)
  269. {
  270.     if (self->styles = style) {
  271.         self->styles = style->next;
  272.     return self;
  273.     } else {
  274.         HTStyle * scan;
  275.     for(scan = self->styles; scan; scan = scan->next) {
  276.         if (scan->next = style) {
  277.             scan->next = style->next;
  278.         return self;
  279.         }
  280.     }
  281.     }
  282.     return 0;
  283. }
  284.  
  285. /*    Create new style sheet
  286. */
  287.  
  288. HTStyleSheet * HTStyleSheetNew NOARGS
  289. {
  290.     HTStyleSheet * self = (HTStyleSheet *)malloc(sizeof(*self));
  291.  
  292.     memset((void*)self, 0, sizeof(*self));    /* ANSI */
  293. /* Harbison c ref man says (char*)self
  294.    but k&r ansii and abc books and Think_C say (void*) */
  295.     
  296. /*    bzero(self, sizeof(*self)); */        /* BSD */
  297.     return self;
  298. }
  299.  
  300.  
  301. /*    Free off a style sheet pointer
  302. */
  303. HTStyleSheet * HTStyleSheetFree ARGS1 (HTStyleSheet *,self)
  304. {
  305.     HTStyle * style;
  306.     while((style=self->styles)!=0) {
  307.         self->styles = style->next;
  308.     HTStyleFree(style);
  309.     }
  310.     free(self);
  311.     return 0;
  312. }
  313.  
  314.  
  315. /*    Read a stylesheet from a typed stream
  316. **    -------------------------------------
  317. **
  318. **    Reads a style sheet from a stream.  If new styles have the same names
  319. **    as existing styles, they replace the old ones without changing the ids.
  320. */
  321.  
  322. #ifdef NEXT_SUPRESS  /* Only on the NeXT */
  323. HTStyleSheet * HTStyleSheetRead(HTStyleSheet * self, NXStream * stream)
  324. {
  325.     int numStyles;
  326.     int i;
  327.     HTStyle * style;
  328.     char styleName[80];
  329.     NXScanf(stream, " %d ", &numStyles);
  330.     if (TRACE) fprintf(stderr, "Stylesheet: Reading %d styles\n", numStyles);
  331.     for (i=0; i<numStyles; i++) {
  332.         NXScanf(stream, "%s", styleName);
  333.         style = HTStyleNamed(self, styleName);
  334.     if (!style) {
  335.         style = HTStyleNewNamed(styleName);
  336.         (void) HTStyleSheetAddStyle(self, style);
  337.     }
  338.     (void) HTStyleRead(style, stream);
  339.     if (TRACE) HTStyleDump(style);
  340.     }
  341.     return self;
  342. }
  343.  
  344. /*    Write a stylesheet to a typed stream
  345. **    ------------------------------------
  346. **
  347. **    Writes a style sheet to a stream.
  348. */
  349.  
  350. HTStyleSheet * HTStyleSheetWrite(HTStyleSheet * self, NXStream * stream)
  351. {
  352.     int numStyles = 0;
  353.     HTStyle * style;
  354.     
  355.     for(style=self->styles; style; style=style->next) numStyles++;
  356.     NXPrintf(stream, "%d\n", numStyles);
  357.     
  358.     if (TRACE) fprintf(stderr, "StyleSheet: Writing %d styles\n", numStyles);
  359.     for (style=self->styles; style; style=style->next) {
  360.         NXPrintf(stream, "%s ", style->name);
  361.     (void) HTStyleWrite(style, stream);
  362.     }
  363.     return self;
  364. }
  365. #endif
  366.